import { message } from "antd";
import axios from "axios";
import history from "./history";
import { getToken, removeToken } from "./token";
const instance = axios.create({
  baseURL: 'http://geek.itheima.net',
  timeout: 5000,
})

// 请求拦截器
instance.interceptors.request.use(
  function (config) {
    const token = getToken()
    if (token) {
      config.headers.Authorization = 'Bearer ' + token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 响应拦截器
instance.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    // console.log(28, error.response.data.message);
    if (error.response && error.response.status === 401) {
      // window.location.href = "/"
      removeToken()
      history.push('/')
      message.warning(error.response.data.message, 1.5)
      // console.log(33, history.push);
    }
    return Promise.reject(error)
  }
)

export default instance